﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Utils.Explorer
{
    public class Tasks
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static ArrayList GenerateFileList(string dir)
        {
            return GenerateFileList(dir, false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static ArrayList GenerateFileList(string dir, bool save_directories)
        {
            ArrayList files = new ArrayList();
            bool Empty = true;
            if (Directory.Exists(dir))
            {
                if(save_directories)
                    files.Add(dir);

                try
                {
                    foreach (string file in Directory.GetFiles(dir)) // add each file in directory
                    {
                        files.Add(file);
                        Empty = false;
                    }
                }
                catch (UnauthorizedAccessException ex) { }

                if (Empty)
                {
                    if (Directory.GetDirectories(dir).Length == 0)
                    // if directory is completely empty, add it
                    {
                        files.Remove(dir);
                        files.Add(dir + @"/");
                    }
                }

                foreach (string dirs in Directory.GetDirectories(dir)) // recursive
                {
                    foreach (object obj in GenerateFileList(dirs, save_directories))
                    {
                        files.Add(obj);
                    }
                }                
            }
            return files;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filetypes"></param>
        /// <returns></returns>
        public static ArrayList FilterFileList(ArrayList list, ArrayList file_types)
        {
            ArrayList new_list = new ArrayList();

            foreach(string str in list)
            {
                if (file_types.Contains(Path.GetExtension(str).ToString()))
                    new_list.Add(str);
            }

            return new_list;
        }

        public static ArrayList FilterDirectoryList(ArrayList list)
        {
            ArrayList new_list = new ArrayList();

            foreach (string str in list)
            {
                if (Directory.Exists(str))
                    new_list.Add(str);
            }

            return new_list;
        }
    }
}
